package com.kk.emp.service.impl;
/*
 *@author KK on 2022.10.08 0:11
 */

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kk.emp.entity.Emp;
import com.kk.emp.dao.EmpMapper;
import com.kk.emp.service.EmpService;
import com.kk.emp.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public boolean addEmp(Emp emp) {
        if (empMapper.addEmp(emp)>0) {
            return true;
        }
        return false;
    }

    @Override
    public Emp doLogin(String ename, String pwd) {
        if (StringUtils.isEmpty(ename)||StringUtils.isEmpty(pwd)){
            return null;
        }
        Emp emp = empMapper.getByName(ename);
        if (emp==null){
            return null;
        }
        if (emp.getPwd().equals(pwd)){
            return emp;
        }
        return null;
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public PageInfo<Emp> getEmpByPage(Map<String, String> emps) {
        // 获取 转换 当前页面
        int num = StringUtil.stringToInt(emps.get("pageNum"));
        // 创建pageHelper
        PageHelper.startPage(num, 5);
        List<Emp> all = empMapper.getEmpByKey(emps);
        PageInfo<Emp> info = new PageInfo<>(all);
        return info;
    }

    @Override
    public boolean delEmp(Integer eno) {
        if (empMapper.delEmp(eno)>0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean checkName(String ename) {
        if (StringUtil.isNotEmpty(ename)) {
            if (empMapper.getByName(ename) != null) {
                return true;
            }
        }
        return false;
    }

}
